package org.mpremote.android.framework;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.mpremote.android.R;
import org.mpremote.android.framework.xml.Albums;
import org.mpremote.android.framework.xml.ArtistAlbums;
import org.mpremote.android.framework.xml.Artists;
import org.mpremote.android.framework.xml.MPXmlParser;

import java.io.InputStream;
import java.net.URI;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 31/08/2010
 * Time: 21:15:58
 */
public class MPWebservicesConsumer {

    private final String mpIp;
    private final String url;
    private final Context applicationContext;

    public MPWebservicesConsumer(Context applicationContext) {
        this.applicationContext = applicationContext;
        SharedPreferences sharedPreferences = applicationContext.getSharedPreferences(applicationContext.getString(R.string.mp_properties_preferences_file), 0);
        this.mpIp = sharedPreferences.getString(applicationContext.getString(R.string.mp_properties_mp_ip), null);
        String mpPort = sharedPreferences.getString(applicationContext.getString(R.string.mp_properties_mp_port), null);
        this.url = String.format("http://%s:%s", mpIp, mpPort);

    }

    public Artists getArtistsList() {
        final InputStream inputStream = executeRelativeGetWebservice(applicationContext.getString(R.string.mp_ws_url_artists));
        if (inputStream != null) {
            return MPXmlParser.parseXML(inputStream);
        }
        return null;
    }

    public ArtistAlbums getAlbumListForArtist(String url) {
        final InputStream inputStream = executeGetWebservice(url.replace("*", mpIp));
        if (inputStream != null) {
            return MPXmlParser.parseXML(inputStream);
        }
        return null;
    }

    public Albums getAlbumsList() {
        final InputStream inputStream = executeRelativeGetWebservice(applicationContext.getString(R.string.mp_ws_url_albums));
        if (inputStream != null) {
            return MPXmlParser.parseXML(inputStream);
        }
        return null;
    }

    private InputStream executeRelativeGetWebservice(String pathToWs) {
        DefaultHttpClient client = new DefaultHttpClient();
        try {
            URI uri = new URI(url + pathToWs);
            Log.i(this.getClass().getSimpleName(), "Execucting GET for: " + uri.toString());
            final HttpGet request = new HttpGet(uri);
            final HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                Log.i(this.getClass().getSimpleName(), "Done for " + uri.toString());
                return response.getEntity().getContent();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private InputStream executeGetWebservice(String url) {
        DefaultHttpClient client = new DefaultHttpClient();
        try {
            URI uri = new URI(url);
            final HttpGet request = new HttpGet(uri);
            final HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return response.getEntity().getContent();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
}
